<?php
class PaymentModel extends BaseModel implements Model {
    const ST_PAYED = 2;

    function __construct() {
        parent::__construct("payment", "payment_id");
        $this->addField("payment_id", Model::TYPE_INT, Model::MOD_PRIMARY);
        $this->addField("company_id", Model::TYPE_INT);
        $this->addField("object_id", Model::TYPE_INT);
        $this->addField("pay_deadline", Model::TYPE_DATE);
        $this->addField("payment_subject", Model::TYPE_VARCHAR);
        $this->addField("invoice_no", Model::TYPE_VARCHAR);
        $this->addField("status", Model::TYPE_INT);
    }

    public function renderAlias($record) {
        $result = $record;
        return $result;
    }

    public function getInvoiceData($id) {
        $data = $this->load($id);
        $query = "SELECT * FROM payment WHERE invoice_no='" . $data['invoice_no'] . "'";
        $res = Database::executeQuery($query);
        return Database::getDataFromResource($res);
    }

    public function removeInvoice($id) {
        $data = $this->load($id);
        $query = "DELETE FROM payment WHERE invoice_no='" . $data['invoice_no'] . "'";
        Database::executeQuery($query);
    }

    public function removePayedInvoicesFor($companyId) {
        $query = "DELETE FROM payment WHERE status=" . self::ST_PAYED . " AND company_id=$companyId";
        Database::executeQuery($query);
    }

    public function addObjectSponsorData($companyId, $objectId, $invoiceId, $spn) {
        $data = array();
        foreach ($spn as $key=>$value) {
            if ($key == "spn_service_selection") {
                $data[] = array(
                    company_id => $companyId,
                    object_id => $objectId,
                    pay_deadline => $value,
                    payment_subject => "service_select",
                    invoice_no => $invoiceId,
                );
            } else if ($key == "spn_service_logo") {
                $data[] = array(
                    company_id => $companyId,
                    object_id => $objectId,
                    pay_deadline => $value,
                    payment_subject => "service_logo",
                    invoice_no => $invoiceId,
                );
            } else if ($key == "spn_bl_photo") {
                $data[] = array(
                    company_id => $companyId,
                    object_id => $objectId,
                    pay_deadline => $value,
                    payment_subject => "product_photo",
                    invoice_no => $invoiceId,
                );
            }
        }
        if (count($data) > 0) {
            $query = QueryHelper::bulkInsertInto("payment", $data);
            Database::executeQuery($query);
        }
    }

    public function addCompanySponsorData($companyId, $invoiceId, $spn) {
        $data = array();
        foreach ($spn as $key=>$value) {
            if ($key == "spn_export_rotator") {
                $data[] = array(
                    company_id => $companyId,
                    pay_deadline => $value,
                    payment_subject => "export_rotator",
                    invoice_no => $invoiceId,
                );
            } else if ($key == "spn_import_rotator") {
                $data[] = array(
                    company_id => $companyId,
                    pay_deadline => $value,
                    payment_subject => "import_rotator",
                    invoice_no => $invoiceId,
                );
            } else if ($key == "spn_bl_rotator") {
                $data[] = array(
                    company_id => $companyId,
                    pay_deadline => $value,
                    payment_subject => "businesslead_rotator",
                    invoice_no => $invoiceId,
                );
            } else if ($key == "spn_service_rotator") {
                $data[] = array(
                    company_id => $companyId,
                    pay_deadline => $value,
                    payment_subject => "service_rotator",
                    invoice_no => $invoiceId,
                );
            } else if ($key == "spn_ha_rotator") {
                $data[] = array(
                    company_id => $companyId,
                    pay_deadline => $value,
                    payment_subject => "ha_rotator",
                    invoice_no => $invoiceId,
                );
            } else if ($key == "spn_ha_countrysearch") {
                $data[] = array(
                    company_id => $companyId,
                    pay_deadline => $value,
                    payment_subject => "ha_countrysearch",
                    invoice_no => $invoiceId,
                );
            } else if ($key == "export_hs4") {
                for ($i = 0; $i < count($value); $i++) {
                    $data[] = array(
                        company_id => $companyId,
                        pay_deadline => $value[$i]['date'],
                        payment_subject => "export_hs4",
                        invoice_no => $invoiceId,
                        hs4_id => $value[$i]['hs4code'],
                    );
                }
            } else if ($key == "import_hs4") {
                for ($i = 0; $i < count($value); $i++) {
                    $data[] = array(
                        company_id => $companyId,
                        pay_deadline => $value[$i]['date'],
                        payment_subject => "import_hs4",
                        invoice_no => $invoiceId,
                        hs4_id => $value[$i]['hs4code'],
                    );
                }
            }
        }
        if (count($data) > 0) {
            $query = QueryHelper::bulkInsertInto("payment", $data);
            Database::executeQuery($query);
        }
    }

    public function getHSOrders($profileId, $hslist) {
        $query = "SELECT * FROM payment WHERE company_id=$profileId AND hs4_id IN ('" . implode("', '", $hslist) . "')";
        $res = Database::executeQuery($query);
        return Database::getDataFromResource($res);
    }

    public function getInvoiceCount() {
        $query = "SELECT count(DISTINCT invoice_no) FROM payment";
        $res = Database::executeQuery($query);
        return Database::getSimpleField($res);
    }

    public function getOrdersFor($companyId) {
        return $this->loadAll("company_id=$companyId");
    }

    public function getInvoices($page, $rowcount) {
        $start = ($page-1) * $reccount;
        $query = "SELECT payment_id, firstname, lastname, companyname, invoice_no FROM payment ".
                 "JOIN companies ON companies.company_id=payment.company_id ".
                 "JOIN users ON users.company_profile_id=companies.company_id ".
                 "WHERE payment.status IS NULL ".
                 "GROUP BY invoice_no ".
                 "LIMIT $start, $rowcount"
        ;
        $res = Database::executeQuery($query);
        $data = Database::getDataFromResource($res);
        return $data;
    }

    public function setStatus($status, $invoiceId) {
        $query = QueryHelper::update("payment", array(
            status => $status,
        ), "payment_id=$invoiceId");
        Database::executeQuery($query);
    }

    public function getPayedInvoices() {
        $query = "SELECT companies.company_id, companyname FROM companies ".
                 "JOIN payment ON payment.company_id=companies.company_id"
        ;
        $res = Database::executeQuery($query);
        return Database::getDataFromREsource($res);
    }

    public function getPayedInvoicesCount() {
        $query = "SELECT count(*) AS c FROM ".
                 "(SELECT companies.company_id, companyname FROM companies ".
                 "JOIN payment ON payment.company_id=companies.company_id)x"
        ;
        $res = Database::executeQuery($query);
        return Database::getSimpleField($res);
    }

    public function isAuthorized($operation, $subject = "") {
        return true;
//        if (Auth::isAdmin()) { return true; }
/*
        switch ($operation) {
            case "update" :
            case "delete" :
            case "addnew" : {
                if (Auth::canChangeDrivers()) { return true; }
            }
            case "list" : {
                if (Auth::canListDrivers()) { return true; }
            }
        }
        return false;
*/
    }

    public function modifyDatabaseInput($data) {
        return $data;
    }
}

?>
